#! /usr/bin/python
# -*- encoding utf-8 -*-
'''
Usage: HttpClient using Socket

'''
import socket

class HttpSocket:
    

    def start_client(self, host, port):

        #create a socket
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            print 'Create socket...'
        except socket.error, e:
            print 'socket false:%s'%e

        # connect to server
        try:
            sock.connect((host, port))            
            print 'connect...'
        except socket.error, e:
            print 'connect false %s'%e
            sock.close()

        #send http request
        try:
            print 'send start...'
            sock.send("GET %s HTTP/1.1\r\nHost: %s\r\n\Connection:keep-alive\r\n\r\n" %('/',host))
            print 'Message send sucessfully'
        except socket.error, e:
            print 'send false: %s'%e
            sock.close()

        #revieve data from server
        data=''
        data = sock.recv(1024)
        print data
        flag =  data.split(' ')[1]
        sock.close()
        return flag
     
